package com.wzt.gift.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;


/**
 * 开启资源服务配置
 * 开启全局方法安全校验
 *
 * @author Zenith
 */

// @Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfigurerAdapterV1 implements ResourceServerConfigurer {
    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        // SpringSecurity放行等配置
        httpSecurity.authorizeRequests()
                //校验scope必须为gift ， 对应认证服务的客户端详情配置的clientId
                .antMatchers("/**").access("#oauth2.hasScope('gift')")
                //入住登录放行
                .antMatchers("/tenant/settlement").permitAll()
                //关闭跨域伪造检查
                .and().csrf().disable()
                //把session设置为无状态，意思是使用了token，那么session不再做数据的记录
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }


    @Bean
    @Primary
    public ResourceServerTokenServices resourceServerTokenServices() {
        // 使用远程服务请求授权服务器校验token ， 即：资源服务和授权服务器不在一个主机
        RemoteTokenServices services = new RemoteTokenServices();
        // 授权服务地址 , 当浏览器访问某个资源时就会调用该远程授权服务地址去校验token
        // 要求请求中必须携带token
        services.setCheckTokenEndpointUrl("http://localhost:10050/oauth/check_token");
        // 客户端id，对应认证服务的客户端详情配置的clientId
        services.setClientId("webapp");
        // 密钥，对应认证服务的客户端详情配置的clientId
        services.setClientSecret("123456");
        return services;
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer configurer) throws Exception {

        //核心在配置校验地址
        configurer.resourceId("systemId")
                // 非JWTtoken方式,校验token
                .tokenStore(tokenStore())
                .stateless(true);

    }

    @Bean
    public TokenStore tokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter()); //暂时不用数据库和redis，因为后面要jwttoken不需要存储
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey("com-wzt");
        return jwtAccessTokenConverter;
    }
}
